package com.xframe.domain.sys;

import com.xframe.domain.BaseModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.xframe.xdal.core.annotation.*;
import com.xframe.xdal.core.autofill.IAutoFillBean;
import com.xframe.xdal.core.constant.AutoFillType;
import lombok.Data;
import lombok.experimental.Accessors;


/**
 * 系统实体字段处理器子表
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-03-28
 */
@Data
@Accessors(chain = true)
@XSelectItem(keyName = "valprocId",codeName = "valprocId",textName = "customType")
@XTable(value = "SYS_MODEL_ATTR_VALPROC", label = "字段处理器子表")
@XBelongsTo(typeName = "com.xframe.domain.sys.SysModelAttr",foreignKey = "attrId", targetKey = "attrId",as = "refSysModelAttr")
public class SysModelAttrValProc extends BaseModel implements IAutoFillBean {

    /**
     * 字段处理器ID
     */
    @AutoFill(AutoFillType.Snowflake)
    @XColumn(name = "VALPROC_ID", primaryKeyFlag = true,desc = "字段处理器ID")
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    private Long valprocId;

    /**
     * 模型字段ID
     */
    @XColumn(name = "MODEL_ATTR_ID",desc = "模型字段ID")
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    private Long attrId;

    /**
     * 模型ID
     */
    @XColumn(name = "MODEL_ID",desc = "模型ID")
    @JsonSerialize(using = com.fasterxml.jackson.databind.ser.std.ToStringSerializer.class)
    private Long modelId;

    /**
     * 自动填充类型
     */
    @XColumn(name = "AUTO_TYPE",desc = "模型ID",length = 20)
    private String autoType;

    /**
     * 填充默认值
     */
    @XColumn(name = "DEF_VAL",desc = "填充默认值",length = 20)
    private String defVal;

    /**
     * 自动填充参数
     */
    @XColumn(name = "AUTO_ARGS",desc = "自动填充参数",length = 20)
    private String args;

    /**
     * 自定义填充类型
     */
    @XColumn(name = "AUTO_CUSTOM_TYPE",desc = "自定义填充类型",length = 20)
    private String customType;

    /**
     * 是否强制覆盖值
     */
    @AutoFill(defVal = "0")
    @XColumn(name = "OVERWRITE_FLAG",desc = "是否强制覆盖值")
    private Integer overwriteFlag;

    /**
     * 排序
     */
    @AutoFill(defVal = "99")
    @XColumn(name = "SORT_NO",desc = "排序")
    private Integer sortNo;

    /**
     * 执行的时机  C R U
     */
    @XColumn(name = "OP_MARK",desc = "执行的时机")
    private Integer opMark;

    @JsonIgnore
    private SysModelAttr refSysModelAttr = null;
//    @Override
//    public int compareTo(SysModelAttrValProc attrValProc) {
//        return this.sortNo - attrValProc.sortNo;
//    }

    @Override
    public AutoFillType getAutoFillType() {
        try {
            return AutoFillType.valueOf(this.autoType);
        }catch (Exception exception) {
            exception.printStackTrace();
            return AutoFillType.Value;
        }
    }

    @Override
    public String getType() {
        return this.customType;
    }

    @Override
    public Boolean getOverwrite() {
        return this.overwriteFlag == 1;
    }

    @Override
    public Integer getOperateMark() {
        return this.opMark;
    }

    @Override
    public Integer getOrder() {
        return sortNo;
    }

    @Override
    public Class<?> getFieldType() {
        if(refSysModelAttr == null){
            return String.class;
        }
        return refSysModelAttr.getDataType();
    }

    @Override
    public int compareTo(IAutoFillBean attrValProc) {
        return this.sortNo - attrValProc.getOrder();
    }
}
